package com.example.up;

import java.io.File;

import com.example.up.util.Content;
import com.example.up.util.HttpUtil;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AppReceiver extends BroadcastReceiver {
	private static final int PACKAGE_NAME_START_INDEX = 8;
	private Context con;

	@Override
	public void onReceive(Context context, Intent intent) {
		con = context;
		if (intent == null) {
			return;
		}

		if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
			String data = intent.getDataString();

			if (data == null || data.length() <= PACKAGE_NAME_START_INDEX) {
				return;
			}

			String packageName = data.substring(PACKAGE_NAME_START_INDEX);

			if (packageName.equals(Content.packger)) {

				clean();

				android.os.Process.killProcess(android.os.Process.myPid());
				System.exit(0);
			}
		}

	}

	public void clean() {
		File f = new File(Content.path);

		File[] fl = f.listFiles();
		for (int i = 0; i < fl.length; i++) {
			if (fl[i].toString().endsWith(".apk")) {
				if (fl[i].delete()) {
					Toast.makeText(con, con.getString(R.string.c_success),
							Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(con, con.getString(R.string.c_failue),
							Toast.LENGTH_LONG).show();
				}
			}
		}
	}

}